package com.java.dormitorymanager.mvc.view;

import com.java.dormitorymanager.pojo.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

/**
 * @program: DormitoryManager
 * @description: 宿舍详情界面
 * @author: ShuangChenYue
 * @create: 2021-05-18 09:30
 **/
@WebServlet(value = "/ViewDormDetail")
public class ViewDormDetailServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        System.out.println("点击详情处理完数据后来到该显示界面");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();

        out.println("<!DOCTYPE html>");
        out.println("<html lang=\"en\">");
        out.println("<head>");
        out.println("<meta charset=\"UTF-8\">");
        out.println("<title>宿舍人员详情</title>");
        out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"http://localhost:8080/DormitoryManager/css/css-dorm/showDormDetail.css\">");
        out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"http://localhost:8080/DormitoryManager/css/css-other/doIcon.css\">");
        out.println("</head>");
        out.println("<body>");
        out.println("<div id=\"divTitle\">");
        out.println("<h1 id=\"h01\">Manage Dorm</h1>");
        out.println("</div>");
        out.println("<div id=\"div02\">");
        out.println("<form>");
        out.println("<input id=\"inputSearch01\" type=\"text\" name=\"name\" placeholder=\"输入学生姓名查找\"><input class=\"iconfontU\" id=\"inputSearch02\" type=\"submit\" name=\"search\" value=\"&#xe632;\" title=\"点击搜索\">");
        out.println("</form>");
        out.println("</div>");
        out.println("<div id=\"div01\">");
        String dornum = (String) req.getAttribute("dornum");
        out.println("<h3 class=\"ListName\">"+dornum+"</h3>");
        out.println("<table class=\"table01\">");
        out.println("<tr class=\"tr01\">");
        out.println("<td>ID</td>");
        out.println("<td>姓名</td>");
        out.println("<td>密码</td>");
        out.println("<td>学号</td>");
        out.println("<td>年龄</td>");
        out.println("<td>性别</td>");
        out.println("<td>邮箱</td>");
        out.println("<td>状态</td>");
        out.println("<td>宿舍</td>");
        out.println("<td>操作</td>");
        out.println("</tr>");

        List<Student> allStudentsByDornumList = (List<Student>) req.getAttribute("allStudentsByDornumList");
        Iterator<Student> iterator = allStudentsByDornumList.iterator();
        while (iterator.hasNext())
        {
            Student student = iterator.next();
            out.println("<tr class=\"tr02\">");
            out.println("<td style=\"text-align: center\">"+student.getId()+"</td>");
            out.println("<td>"+student.getName()+"</td>");
            out.println("<td>"+student.getPassword()+"</td>");
            out.println("<td>"+student.getStunum()+"</td>");
            out.println("<td>"+student.getAge()+"</td>");
            out.println("<td>"+student.getGender()+"</td>");
            out.println("<td>"+student.getEmail()+"</td>");
            out.println("<td>"+student.getStatus()+"</td>");
            out.println("<td>"+student.getDornum()+"</td>");
            out.println("<td><a href=\"/DormitoryManager/mvc/deleteMSFromDorm.do?id="+student.getId()+"&dornum="+student.getDornum()+"\"><i class=\"iconfontU deleteStudentIcon\" title=\"从宿舍删除该学生\">&#xe71d;</i></a></td>");
            out.println("</tr>");
        }

        out.println("</table>");
        out.println("</div>");
        out.println("</body>");
        out.println("</html>");
        out.flush();
        out.close();
    }
}
